// miniprogram/pages/userDetail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    isCoach:false,
    openid: '',
    avatarUrl: '',
    nickName: '',
    isEmpty: false,
    cards: [],
    show: false,
    selectedIndex: '',
    total: 0,
    page: 1,
    pageSize: 20,
    actions: [
      {
        name: '撤销审核',
      }
    ],
  },

  onClose() {
    this.setData({ show: false });
  },
  getMyCardList: function () {
    this.setData({
      page: 1,
      pagesize: 20,
    });
    const db = wx.cloud.database();
    const _ = db.command;
    let openid = this.data.openid
    console.log(openid)
    db.collection('cards').orderBy('systemTime', 'desc').limit(20).where({
      // gt 方法用于指定一个 "大于" 条件，此处 _.gt(30) 是一个 "大于 30" 的条件
      _openid: _.eq(openid)
    }).get({
      success: res => {
        console.log(res.data);
        if (res.data.length == 0) {
          this.setData({ isEmpty: true })
        } else {
          this.setData({ isEmpty: false })
        }
        this.setData({ cards: res.data })
      }
    });
  },
  onClickPreviewImg: function (e) {
    let card = e.currentTarget.dataset.card
    let index = e.currentTarget.dataset.index
    wx.previewImage({
      current:card.images[index],
      urls: card.images,
    })
  },
  onSelect(event) {
    console.log(event.detail);

    wx.showModal({
      title: '确认撤销？',
      content: '撤回审核后需要重新审核，确认撤销吗？',
      confirmText: '确认',
      cancelText: '取消',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title: '撤销中...',
          })
          wx.cloud.callFunction({
            name: 'cancelApproveCard',
            data: {
              id: this.data.selectedIndex,
            },
            success: res => {
              console.log(res)
              if (res.result.success) {
                wx.hideLoading()
                this.getMyCardList()
                wx.showToast({
                  title: '撤销成功',
                  icon: 'success',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: '撤销失败',
                  icon: 'error',
                  duration: 2000
                })
              }
            },
            fail: err => {
              wx.hideLoading()
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let openid = options.openid;
    let avatarUrl = options.avatarUrl;
    let nickName = options.nickName;
    let isCoach = wx.getStorageSync('isCoach')
    this.setData({
      avatarUrl: avatarUrl,
      nickName: nickName,
      openid: openid,
      isCoach:isCoach
    })
    const db = wx.cloud.database()
    db.collection('userInfo').where({
      _openid: openid
    }).get({
      success: res => {
        console.log(res)
        this.setData({ userInfo: res.data[0] })
      },
    })
    this.getMyCardList()
  },
  onClickBtn: function (e) {
    console.log('onClickApprove', e)
    this.setData({ show: true });
    this.setData({ selectedIndex: e.currentTarget.dataset.index });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      pagesize: 20,
    });

    const db = wx.cloud.database();
    const _ = db.command;
    let openid = this.data.openid
    console.log(openid)
    wx.showNavigationBarLoading() //在标题栏中显示加载
    db.collection('cards').orderBy('systemTime', 'desc').limit(20).where({
      // gt 方法用于指定一个 "大于" 条件，此处 _.gt(30) 是一个 "大于 30" 的条件
      _openid: _.eq(openid)
    }).get({
      success: res => {
        console.log(res.data);
        if (res.data.length == 0) {
          this.setData({ isEmpty: true })
        } else {
          this.setData({ isEmpty: false })
        }
        this.setData({ cards: res.data })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    this.setData({
      page: this.data.page,
      pagesize: 20
    });
    const db = wx.cloud.database();
    const _ = db.command;
    let openid = this.data.openid
    let skip = (this.data.page - 1) * 20
    console.log("skip", skip)
    db.collection('cards').orderBy('systemTime', 'desc').skip(skip).limit(20).where({
      // gt 方法用于指定一个 "大于" 条件，此处 _.gt(30) 是一个 "大于 30" 的条件
      _openid: _.eq(openid)
    }).get({
      success: res => {
        console.log(res)
        if (res.data.length == 0) {
          this.data.page--
          this.setData({
            page: this.data.page
          });
        } else {
          this.setData({ cards: this.data.cards.concat(res.data) })
        }
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})